package com.example.mobile2021606127cxz0617homework;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainInfor;
    private Button btnMainAdd;
    private LinearLayout linearlayout;
    private RecyclerView rv;
    private List<Infor> list = new ArrayList<>();
    MySQLiteOpenHelper openHelper;
    SQLiteDatabase database;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        openHelper = new MySQLiteOpenHelper(this, "factory.db", null, 1);
        database = openHelper.getWritableDatabase();
        Cursor cursor = database.query("staff", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String skill = cursor.getString(cursor.getColumnIndex("skill"));
                int xinchou = cursor.getInt(cursor.getColumnIndex("xinchou"));
                list.add(new Infor(name, skill, xinchou));  //显示在RecyclerView上
            } while (cursor.moveToNext());
        }
        cursor.close();
        database.close();

//        list = new DateAdapter().QueryAll(this);


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(layoutManager);

        RVAdapter adapter = new RVAdapter(list);
        rv.setAdapter(adapter);

        btnMainAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog();
            }
        });

    }

    private void initView() {
        tvMainInfor = (TextView) findViewById(R.id.tv_main_infor);
        btnMainAdd = (Button) findViewById(R.id.btn_main_add);
        linearlayout = (LinearLayout) findViewById(R.id.linearlayout);
        rv = (RecyclerView) findViewById(R.id.rv);
    }

    public void Dialog() {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity2, null, false);
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setView(view);
        Dialog dialog = builder.create();
        EditText addName = view.findViewById(R.id.edt_main_addname);
        EditText addSkill = view.findViewById(R.id.edt_main_addskill);
        EditText addXinchou = view.findViewById(R.id.edt_main_addxinchou);
        Button btnCancel = view.findViewById(R.id.btn_main_cancel);
        Button btnSure = view.findViewById(R.id.btn_main_sure);

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = addName.getText().toString().trim();
                String skill = addSkill.getText().toString().trim();
                int xinchou = Integer.valueOf(addXinchou.getText().toString().trim());
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("skill", skill);
                values.put("xinchou", xinchou);
                database = openHelper.getWritableDatabase();
                database.insert("staff", null, values);
                list.clear();
                Cursor cursor = database.query("staff", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        name = cursor.getString(cursor.getColumnIndex("name"));
                        skill = cursor.getString(cursor.getColumnIndex("skill"));
                        xinchou = cursor.getInt(cursor.getColumnIndex("xinchou"));
                        list.add(new Infor(name, skill, xinchou));
                    } while (cursor.moveToNext());
                }


                cursor.close();
                database.close();
                dialog.dismiss();
            }
        });

        dialog.show();


    }

}